﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;
using System.IO;

namespace Wjtour.Web.Admin.scene
{
    public partial class hoteaddedi : BasePage
    {
        protected DropDownList cid;
        protected TextBox scenename;
        protected HiddenField sceneid;
        protected TextBox img;
        protected FileUpload upload;
        protected Button btnPublish;
        protected Image imgPreview;
        protected Button submit;
        protected TextBox hotelname;
        protected TextBox address;
        protected TextBox opentime;
        protected TextBox decrocatetime;
        protected TextBox reservephone;
        protected TextBox averageprice;
        protected TextBox trafficguide;
        protected Editor introduce;
        protected TextBox hotelstar;

        protected int id = 0;
        protected int hotelid = 0;
        protected int sid = 0;
        Wjtour.Domain.scenehotel scenehotel = new Domain.scenehotel();
        Wjtour.Domain.scene scene = new Domain.scene();
        Wjtour.Domain.hotel hotel = new Wjtour.Domain.hotel();
        scenehotel_bll _bll = new scenehotel_bll();
        scene_bll _sbll = new scene_bll();
        hotel_bll _resbll = new hotel_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            submit.Click += new EventHandler(submit_Click);
            btnPublish.Click += btnPublish_Click;

            if (!IsPostBack)
            {
                BindDropdown();
            }
            if (id > 0)
            {
                scenehotel = _bll.Load(id);
                scene = _sbll.Load(scenehotel.sceneid);
                hotel = _resbll.Load(scenehotel.hotelid);
                sid = scenehotel.sceneid;
                hotelid = scenehotel.hotelid;

                if (!IsPostBack)
                {
                    if (scenehotel != null)
                    {
                        scenename.Text = scene.scenename;
                        sceneid.Value = scenehotel.sceneid.ToString();
                        img.Text = hotel.img;
                        imgPreview.ImageUrl = hotel.img;
                        hotelname.Text = hotel.hotelname;
                        address.Text = hotel.address;
                        opentime.Text = hotel.opentime;
                        reservephone.Text = hotel.reservephone;
                        averageprice.Text = hotel.averageprice.ToString();
                        trafficguide.Text = hotel.trafficguide;
                        introduce.Text = hotel.introduce;
                    }
                }
            }
        }

        void BindDropdown()
        {
            cid.Items.Add(new ListItem(" ┝ 星级酒店", "1"));
            cid.Items.Add(new ListItem(" ┝ 农家乐", "2"));
        }

        void btnPublish_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(upload.FileName))
            {
                Utils.Alert(this.Page, "请选择要上传的文件！");
                return;
            }
            string fileExt = Path.GetExtension(upload.FileName);
            List<string> allowExt = new List<string>();
            allowExt.Add(".jpg");
            allowExt.Add(".png");
            allowExt.Add(".gif");

            if (!allowExt.Contains(fileExt))
            {
                Utils.Alert(this.Page, "系统只允许上传jpg,jpeg,gif的文件！");
                return;
            }
            string uploadDir = Server.MapPath("/uploads");
            if (!Directory.Exists(uploadDir))
            {
                Directory.CreateDirectory(uploadDir);
            }

            string guid = GetFileName("time");
            string filename = guid + fileExt;
            string path = uploadDir + "\\" + filename;
            if (System.IO.File.Exists(filename)) System.IO.File.Delete(path);

            try
            {
                upload.SaveAs(path);
                //string newpath = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename, 100, 100, "jpg");
                imgPreview.ImageUrl = "/uploads/" + filename;
                img.Text = "/uploads/" + filename;
            }
            catch (Exception ex)
            {
                Utils.Alert(this.Page, "文件文件出错，文件大小超过限制或网络原因！");
                return;
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            sid = Utils.StrToInt(sceneid.Value, 0);
            if (sid == 0)
            {
                Utils.Alert(this.Page, "请选择景点！");
                return;
            }

            hotel.id = hotelid;
            hotel.img = img.Text;
            hotel.hotelname = hotelname.Text;
            hotel.address = address.Text;
            hotel.opentime = opentime.Text;
            hotel.reservephone = reservephone.Text;
            hotel.averageprice = Utils.StrToDouble(averageprice.Text, 0);
            hotel.trafficguide = trafficguide.Text;
            hotel.introduce = introduce.Text;
            if (hotel.id == 0)
                hotel.addtime = DateTime.Now;

            bool b = _bll.Save(hotel, sid);
            if (b)
            {
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "hotelist.aspx", false);
            }
        }
    }
}